package com.hankol.newretail.adapter.details


import com.baselibrary.extension.checkNotNull
import com.baselibrary.extension.checkValue
import com.baselibrary.extension.load
import com.baselibrary.view.CircleImageView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.hankol.newretail.R
import com.hankol.newretail.bean.goods_details.FictitiousPersonBean
import java.util.*
import kotlin.collections.ArrayList
import kotlin.random.Random.Default.nextInt

class RetailsAutoScrollAdapter : BaseQuickAdapter<FictitiousPersonBean.FictitiousPerson, BaseViewHolder>(R.layout.item_auto_scroll_retail) {

    private val list: ArrayList<String> = arrayListOf("下单了商品", "加购了商品", "浏览了商品", "分享了商品")

    override fun convert(helper: BaseViewHolder, item: FictitiousPersonBean.FictitiousPerson) {
        helper.setIsRecyclable(false)
        val r = Random().nextInt(3) + 1 // random.nextInt(3)只能生成[0,3)

        helper.setText(R.id.tv_fictitious_content, "${encryptionName(item.name)} ${list[r]}")
        helper.getView<CircleImageView>(R.id.circle_fictitious_head).load(mContext, item.icon.checkValue())
    }


    override fun getItem(position: Int): FictitiousPersonBean.FictitiousPerson {
        val newPosition = position % data.size
        return data[newPosition]
    }

    override fun getItemViewType(position: Int): Int {
        var count = headerLayoutCount + data.size
        //刚开始进入包含该类的activity时,count为0。就会出现0%0的情况，这会抛出异常，所以我们要在下面做一下判断
        if (count <= 0) {
            count = 1
        }
        val newPosition = position % count;
        return super.getItemViewType(newPosition);
    }

    override fun getItemCount(): Int {
        return Integer.MAX_VALUE
    }

    fun encryptionName(name: String): String {
        return if (name.length > 1) {
            val toCharArray = name.toCharArray()
            if (toCharArray.size >= 2) {
                return "${toCharArray[0]}**${toCharArray[toCharArray.size - 1]}"
            }
            return "X**"
        } else {
            return if (name.checkNotNull()) {
                "${name.toCharArray()[0]}**"
            } else {
                "X**"
            }
        }
    }
}